package com.csy.tcshop.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.csy.tcshop.order.vo.PayVo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021002128663225";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCkATZDH/xhCmsnHx7inHH78CXmcoX/Ih/P7OFVBdfZr0yZjUtbBk/dJFL7MMj6KQl5BOVAjWZkKJff1OgkXyvXijxFrnh+tq5gMHOr7rGNmm8+B/s+LuuFP+EPpEiGHjwBv849cyHJOaFS6PBx5U6bxpgVQHn+OGJyh+zH0HuoXKBXdqNxNWVJ+ycdKVuW3p/HzHFfw7//lPWty8Wk798c8lM/b9szXOMmD0xarBECimvRgSsNFS8wO7Oy4IZZ/FPzf8r4qPWS0xmkUB8s+HcgafW2tkGdqkOgEGjCCoWhOS8ieBaRJaEr7FHsyL3fOcg6W/no1wrCrK9yZD4VpWW1AgMBAAECggEBAIkKYjcQwSitNgBIptwgcbZdYq3/QCyCbSam/1q2OTkym8UrJa53xcA5nAU9LJKsO7gd+RpJjSdgLBfipd/OjqnaXqazNQhcybSih+VA3KhARXgF31CEkFSVnhAd7GP1W5rylPkP/UlRmJXZAyGIxHiyJ16Ks+VTZB/jqF0Gvm1dMkAufunzsm5UF9F8mrpoivS/0r83qhjuynZRhHMEYwwKPrxAqgj+CTQ/sEy0o876nzLbQCp6YETmpVihCbEM/CJr1aqtGUwYtrurf5UQrxvgZA+xMixDOQ7TZQgEo2j7lCVRUJRe4ucEUAhenIYT8JCxozgZLbmS/kBj6I0vwDkCgYEAz3SjJ6dIiTA6JBPBA9sCxvIN65huaylIpdnouVuxrd5WoDdOqTweiP1rOsAdVsrLFVDutbwcer00xawjgEdktPMYVUxhdr6IlvGp7Ap3rKJQN8/kGgkQ0K3zOOLjFMsPSLyYuuuVA3Z+o1Lfi88qW4EmHn4wC94c5IkeYawIRFsCgYEAymG0kZ4tA7u0UucOLLY5csTrthedsNV4n0ZkE9jup8t7LZCK2mPijrFKK5f5AFdWaDZ0xMeFcK2F3hHtkKkbhNzunce3dNaeeZbvonUMjBdQXxsujoyxSMBClfsZ9h29O/hdHoe7pWT9FJxnfC7H3DkmDogl9p/SCPUsk02l2y8CgYAUjnw0S7E2Vt8MACEaHrORvx59/+bBG6AlPwh+hYV/sw5mZdSXlGt6K5JaifreRM4G2LQusesohmMaS0phRbHG9Geu3Bi0SYEDZALwbsEZDCRiw2jY45kU9IerVWAnCVAEZxvDOeDULQOzb1zTX73Uv2FlmB9urZ2zT5sBFqjjDQKBgQCwvQzlEl67YOQEdjXBxE19Ng2nEw6qTHKvndhuIMeij6f9zboFKK/E730Xfvt8Wcli7xOgeaHGGjPLEJsH9zbKG+rirXRAO+Ffy/lwRYn0mbOdqu5InQOBxi0gL46niaclxSsFK4Iq2XvO0Rvqrivp3ZqnnCyTKVVSb1JDPc83DwKBgHxczvSN6m9JbDksM0TnUopusCEL53MAtx18/6ymPiqK25BjWsR+k0WNjKYWQlWOEXjv8I/EfNVaLeImUJ3hIxKixhvq4Fsmhu54hBwCnSgPP2veZfKL2Meyveuev9T1rmTDmn88k+hnu2szCviL0NxNbLB9fQy/UA5mE6C/liLj";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq2LSrj9CXoNmOcK91M9tbNcnCne2t2NjXpvy/JAWa1R0fgoqvdVa3iKOr/OAjSmcFYy12oo7BDOrqhLvkUca7rDV09R+sr1UPC5pOGTIHlpDg4aP92wWcyeR1Xtho/l4ASLIksx7W6ORmxAeLLFNOKMrEJra1nbpjlPqY+7RlHCQUQYlMOatIJf3jfqBUrCsZZsrCmAbvnUxySQCfoaA2XRy9kXbqfwRmG3n8lWS8YqdCWKrAX6ibOs5xxLV+E7eTybs6FOih0UKvsqR4GoZluHrqepmX8tFyNRa4YRqxxYpIz0YgVIxFfqJQRxwtORHSAHBPj0Kvzij1WImvR3KoQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://sb9pg3.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.tcshop.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
